package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.DineFoodDao;
import com.qububu.play.dineTogether.dto.ActionFoodDto;
import com.qububu.play.dineTogether.dto.ActionFoodDto.StapleFood;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Repository
public class DineFoodDaoImpl implements DineFoodDao{
	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public List<ActionFoodDto> getActionFoodListByActionId(int actionIdInt) {
		List<ActionFoodDto> foodList = null;
		foodList = new ArrayList<ActionFoodDto>();
		
		String sql = "select * from action_food where action_id = ?";
		List<Map<String, Object>> foodMap = jdbcTemplate.queryForList(sql, actionIdInt);
		ActionFoodDto actionFoodDto = null;
		for (Map<String, Object> elem : foodMap) {
			actionFoodDto = new ActionFoodDto((Long)elem.get("food_id"), (Long)elem.get("action_id"));
			actionFoodDto.setFoodName(String.valueOf(elem.get("food_name")));
			actionFoodDto.setFoodPic(String.valueOf(elem.get("food_pic")));
			actionFoodDto.setStapleFood(StapleFood.getStapleFood( (Integer) elem.get("staple_food")));
			foodList.add(actionFoodDto);
		}
		
		return foodList;
	}
	
	
	public void doAddActionFood(ActionFoodDto actionFoodDto) {
		String foodSql = "insert into action_food(staple_food, food_pic, food_name, action_id) values(?, ?, ?, ?)";
		int actionId = ParamConverter.convertToInteger(SystemUtil.decrypt(actionFoodDto.getActionId()));
		Object[] addActionFood = {
				actionFoodDto.getStapleFood().toIntValue(),
				actionFoodDto.getFoodPic(),
				actionFoodDto.getFoodName(),
				actionId
		};
		jdbcTemplate.update(foodSql, addActionFood);
	}

	public ActionFoodDto getActionFoodInfoByFoodId(int foodIdInt) {
		String sql = "select * from action_food where food_id = ?";
		
		Map<String, Object> foodInfoMap = jdbcTemplate.queryForMap(sql, foodIdInt);
		ActionFoodDto actionFoodDto = new ActionFoodDto((Long)foodInfoMap.get("food_id"), (Long)foodInfoMap.get("action_id"));
		actionFoodDto.setFoodName(String.valueOf(foodInfoMap.get("food_name")));
		actionFoodDto.setFoodPic(String.valueOf(foodInfoMap.get("food_pic")));
		actionFoodDto.setStapleFood(StapleFood.getStapleFood( (Integer) foodInfoMap.get("staple_food")));
		
		return actionFoodDto;
	}

	public void doModifyActionFood(ActionFoodDto actionFoodDto) {
		String sql = "update action_food set staple_food = ?, food_pic = ?, food_name = ? where food_id = ?";
		Object[] modifyFood = {
				actionFoodDto.getStapleFood().toIntValue(),
				actionFoodDto.getFoodPic(),
				actionFoodDto.getFoodName(),
				actionFoodDto.getFoodId()
		};
		
		jdbcTemplate.update(sql, modifyFood);
	}

	public boolean doDeleteActionFoodByFoodId(int foodIdInt) {
		String sql = "delete from action_food where food_id = ?";
		jdbcTemplate.update(sql, foodIdInt);
		
		return true;
	}
}
